﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility.Peripheral;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;


namespace ConfExpert.Forms.Depo
{
    public partial class BenzerKumas : XForm
    {

        public BenzerKumas()
        {
            InitializeComponent();
        }

        private void BenzerKumas_Load(object sender, EventArgs e)
        {
            btnTara.ItemClick += btnTara_ItemClick;

            gridView1.Init("GetBenzerRenk2Temp", 0);
            gridView1.SetCaptions("ProductFeatureTempId:Id");
            gridView1.Columns["P"].ToolTip = "Scan";
            gridView1.FormatAsDateTime("InsertedOn");
            gridView1.BestFitColumns();
            gridView1.MoveLast();

            gridView1.RowCellClick += gridView1_RowCellClick;

            GridManager gm = GridManager.GetManager(gridView1);
            gm.GridMenu.AddMenu("Benzerlerini Göster", Goster);
        }

        void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            GridView view = sender as GridView;

            if (e.RowHandle > -1)
            {
                DataRow dr = gridView1.GetDataRow(gridView1.FocusedRowHandle);
                int ProductFeatureTempId = dr["ProductFeatureTempId"].ToInt();

                if (e.Column.FieldName == "P")
                    if (ProductFeatureTempId > 0)
                        using (Detail500 dv = new Detail500(ProductFeatureTempId))
                            dv.ShowDialog();
            }
        }

        void Goster(object sender, EventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(gridView1.FocusedRowHandle);
            int Id = dr["ProductFeatureTempId"].ToInt();
            FindSimilarBenzerRenk(Id, 9);
        }

        Scanner.PhotosTable photosTable = new Scanner.PhotosTable();
        void btnTara_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;

                Image scanned = Scanner.Scan();    //kumaş için sabitlendi

                //Image scanned = Image.FromFile("D:\\500\\68075.jpg");
                //byte[] bytes =  File.ReadAllBytes("d:\\test.pmg");
                photosTable = new Scanner.PhotosTable(true);
                DataRow dr = photosTable.AddRow(0, scanned, false);

                Image image = photosTable.GetPhoto500();

                Session.ServerAddress = "confexpert.karbel.com";
                cePhotoServices.PhotoService ps = new cePhotoServices.PhotoService();

                double[] d = ps.ExtractFeatures(image.ToByteArray());

                for (int i = 0; i < 47; i++)
                    photosTable.Table.Rows[0][string.Format("F{0:00}", i)] = d[i];

                int Id = ExecuteScalar("UpdPhoto6Temp", photosTable.Table, WhoAmI).ToInt();
                gridView1.RefreshDataSource("GetBenzerRenk2Temp", 0);
                gridView1.MoveLast();
                FindSimilarBenzerRenk(Id, 9);
                GC.Collect();
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        //int UpdPhoto6(params object[] p)
        //{
        //    using (SqlConnection sqlConn = new SqlConnection(Session.DataManager.ConnectionString))
        //    {
        //        sqlConn.Open();
        //        SqlCommand sqlCmd = sqlConn.CreateCommand();
        //        sqlCmd.CommandText = "UpdPhoto6Temp";
        //        sqlCmd.CommandType = CommandType.StoredProcedure;
        //        SqlParameter[] parameters = new SqlParameter[2];
        //        parameters[0] = new SqlParameter("@tbl", SqlDbType.Structured);
        //        parameters[0].Value = p[0];
        //        parameters[1] = new SqlParameter("@UserId", SqlDbType.Int);
        //        parameters[1].Value = p[1];
        //        sqlCmd.Parameters.AddRange(parameters);
        //        return sqlCmd.ExecuteScalar().ToInt();
        //    }
        //}


    }
}
